package key;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MdeleteUser
 */
@WebServlet("/MdeleteUser")
public class MdeleteUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	Database db = new Database();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MdeleteUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String user = request.getParameter("user");
		int uid = 0;
		try {
			uid = Integer.parseInt(db.getUid(user));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
			uid = 0;
		}
		if(uid == 0){
			/*生成错误页面*/
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			response.setCharacterEncoding("utf-8");
			out.println("<html>\n<head>\n<title>提示信息</title>\n</head>\n<body>");
			out.println("<script type='text/javascript'>\nconfirm('木有这个人的说')");
			out.println("window.location.href='master/index.jsp';\n</script>");
			out.println("</body>\n</html>");
			
//			response.sendRedirect("master/index.jsp");
//			@SuppressWarnings("unused")
//			Frame fr = new Frame("不存在此用户或出现错误");
		} else {
			db.delU(uid);
			db.delT(uid);
			
			/* 生成页面 */
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			response.setCharacterEncoding("utf-8");
			out.println("<html>\n<head>\n<title>提示信息</title>\n</head>\n<body>");
			out.println("<script type='text/javascript'>\nconfirm('" + user +"已干掉')");
			out.println("window.location.href='master/index.jsp';\n</script>");
			out.println("</body>\n</html>");
			
//			response.sendRedirect("master/index.jsp");
//			@SuppressWarnings("unused")
//			Frame fr = new Frame(user+"已干掉");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
